package com.wyd.jwpkglxt_ssm.controller;

import com.wyd.jwpkglxt_ssm.entity.Classroom;
import com.wyd.jwpkglxt_ssm.service.ClassroomService;
import com.wyd.jwpkglxt_ssm.util.ResultCode;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import com.wyd.jwpkglxt_ssm.vo.ProfessionalTitleVo;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (Classroom)表控制层
 *
 * @author wyd
 * @since 2021-08-02 17:34:01
 */
@RestController
@RequestMapping("classroom")
public class ClassroomController {
    /**
     * 服务对象
     */
    @Autowired
    private ClassroomService classroomService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Classroom selectOne(Integer id) {
        return this.classroomService.queryById(id);
    }

    @GetMapping("classroom/page")
    public ResultUtil getAllClassroomByPage(Integer pageNum, Integer pageSize,
                                               @RequestParam(required = false)String classroomName,
                                               @RequestParam(required = false)Integer classroomArea,
                                            @RequestParam(required = false)Integer status) {
        Classroom classroom = new Classroom().setClassroomName(classroomName).setClassroomArea(classroomArea).setStatus(status);
        return ResultUtil.ok().data("pageInfo",this.classroomService.queryAllByPage(pageNum,pageSize,classroom));
    }

    @PostMapping("classroom")
    public ResultUtil addClassroom(@RequestBody Classroom classroom){
        if(!Objects.isNull(classroom)){
            Classroom insertClassroom = this.classroomService.insert(classroom);
            if(!Objects.isNull(insertClassroom)){
                return ResultUtil.ok().data("classroom",insertClassroom);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @PutMapping("classroom")
    public ResultUtil updateClassroom(@RequestBody Classroom classroom){
        System.out.println(classroom);
        if(!Objects.isNull(classroom)){
            Classroom updateClassroom = this.classroomService.update(classroom);
            if(!Objects.isNull(updateClassroom)){
                return ResultUtil.ok().data("classroom",updateClassroom);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @DeleteMapping("classroom/{classroomId}")
    public ResultUtil deleteClassroomById(@PathVariable("classroomId")String ids){
        if(ids.contains("-")){
            String[] idsplit = ids.split("-");
            List<Integer> classroomIds = new ArrayList<>();
            for(String s : idsplit){
                classroomIds.add(Integer.parseInt(s));
            }
            boolean delClassrooms = this.classroomService.deleteClassroomBatch(classroomIds);
            if(delClassrooms){
                return ResultUtil.ok();
            }
        }else if(!"".equals(ids)){
            Integer id = Integer.parseInt(ids);
            boolean delClassroom = this.classroomService.deleteById(id);
            if(delClassroom){
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @PatchMapping("classroom/status")
    public ResultUtil UpdateClassroomStatusBatch(@RequestBody String ids){
        //去掉队头队尾双引号
        String idsr = ids.replace("\"","");
        if(!"".equals(idsr)){
            if(idsr.contains(".usable")){
                String[] split_ids = idsr.split("-");
                split_ids[split_ids.length - 1] = split_ids[split_ids.length - 1].split(".usable")[0];
                List<Integer> classroomIds = new ArrayList<>();
                for(String id : split_ids){
                    classroomIds.add(Integer.parseInt(id));
                }
                int i = this.classroomService.updateStatusBatch(classroomIds, 1);
                if(i > 0){
                    return ResultUtil.ok();
                }else{
                    return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
                }
            }else if(idsr.contains(".safeguard")){
                String[] split_ids = idsr.split("-");
                split_ids[split_ids.length - 1] = split_ids[split_ids.length - 1].split(".safeguard")[0];
                List<Integer> classroomIds = new ArrayList<>();
                for(String id : split_ids){
                    classroomIds.add(Integer.parseInt(id));
                }
                int i = this.classroomService.updateStatusBatch(classroomIds, 0);
                if(i > 0){
                    return ResultUtil.ok();
                }else{
                    return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
                }
            }else if(idsr.contains(".used")){
                String[] split_ids = idsr.split("-");
                split_ids[split_ids.length - 1] = split_ids[split_ids.length - 1].split(".used")[0];
                List<Integer> classroomIds = new ArrayList<>();
                for(String id : split_ids){
                    classroomIds.add(Integer.parseInt(id));
                }
                int i = this.classroomService.updateStatusBatch(classroomIds, 2);
                if(i > 0){
                    return ResultUtil.ok();
                }else{
                    return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
                }
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @GetMapping("classroom")
    public ResultUtil getAllClassroom(Classroom classroom){
        return ResultUtil.ok().data("classrooms",this.classroomService.queryAll(classroom));
    }
}